import { defineStore } from 'pinia';

// 定义类型
export interface NetworkNode {
  id: string;
  label: string;
  type: string;
  weight?: number;
  user?: string;
  description?: string;
}

export interface NetworkLink {
  source: string;
  target: string;
  type?: string;
}

export interface NetworkData {
  nodes: NetworkNode[];
  links: NetworkLink[];
}

export interface RadarData {
  categories: string[];
  values: number[];
}

export interface TimelineData {
  time: string;
  value: number;
  category: string;
}

export interface AnalysisResponse {
  reportId?: string;
  contextSummary?: any;
  interactionAnalysis?: any;
  strategicInsights?: any;
  conflictAssessment?: any;
  optimizationRecommendations?: any;
}

export interface AnalysisState {
  networkData: NetworkData;
  radarData: RadarData;
  timelineData: TimelineData[];
  analysisData: AnalysisResponse | null;
  isLoading: boolean;
  error: string | null;
}

// 创建store
export const useAnalysisStore = defineStore('analysis', {
  state: (): AnalysisState => ({
    networkData: {
      nodes: [],
      links: []
    },
    radarData: {
      categories: [],
      values: []
    },
    timelineData: [],
    analysisData: null,
    isLoading: false,
    error: null
  }),
  
  getters: {
    // 获取网络数据
    getNetworkData: (state) => state.networkData,
    
    // 获取雷达图数据
    getRadarData: (state) => state.radarData,
    
    // 获取时间线数据
    getTimelineData: (state) => state.timelineData,
    
    // 获取分析数据
    getAnalysisData: (state) => state.analysisData,
    
    // 检查是否正在加载
    getLoadingStatus: (state) => state.isLoading,
    
    // 获取错误信息
    getError: (state) => state.error
  },
  
  actions: {
    // 设置网络数据
    setNetworkData(data: NetworkData) {
      this.networkData = data;
    },
    
    // 设置雷达图数据
    setRadarData(data: RadarData) {
      this.radarData = data;
    },
    
    // 设置时间线数据
    setTimelineData(data: TimelineData[]) {
      this.timelineData = data;
    },
    
    // 设置分析数据
    setAnalysisData(data: AnalysisResponse) {
      this.analysisData = data;
      this.finishLoading();
    },
    
    // 开始加载
    startLoading() {
      this.isLoading = true;
      this.error = null;
    },
    
    // 结束加载
    finishLoading() {
      this.isLoading = false;
    },
    
    // 设置错误
    setError(error: string) {
      this.error = error;
      this.isLoading = false;
    },
    
    // 加载分析数据
    async loadAnalysisData() {
      this.startLoading();
      
      try {
        // 这里可以替换为实际的API调用
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1000));
        
        // 模拟网络数据
        const networkData: NetworkData = {
          nodes: [
            { id: '1', label: '目标1', type: 'target', weight: 1 },
            { id: '2', label: '证据1', type: 'evidence', user: 'man', description: '这是证据1的描述' },
            { id: '3', label: '证据2', type: 'evidence', user: 'woman', description: '这是证据2的描述' },
            { id: '4', label: '陈述1', type: 'statement', user: 'man', description: '这是陈述1的描述' }
          ],
          links: [
            { source: '1', target: '2' },
            { source: '1', target: '3' },
            { source: '2', target: '4' },
            { source: '3', target: '4' }
          ]
        };
        
        // 模拟雷达图数据
        const radarData: RadarData = {
          categories: ['情感强度', '话题相关性', '互动频率', '表达清晰度', '回应速度'],
          values: [0.8, 0.6, 0.9, 0.7, 0.5]
        };
        
        // 模拟时间线数据
        const timelineData: TimelineData[] = [
          { time: '2023-01-01', value: 0.2, category: '积极' },
          { time: '2023-01-02', value: 0.3, category: '积极' },
          { time: '2023-01-03', value: -0.1, category: '消极' },
          { time: '2023-01-04', value: 0.5, category: '积极' },
          { time: '2023-01-05', value: 0.7, category: '积极' },
          { time: '2023-01-06', value: -0.3, category: '消极' },
          { time: '2023-01-07', value: 0.1, category: '积极' }
        ];
        
        // 模拟分析数据
        const analysisData: AnalysisResponse = {
          reportId: '123456',
          contextSummary: '这是上下文总结',
          interactionAnalysis: '这是交互分析',
          strategicInsights: '这是战略洞察',
          conflictAssessment: '这是冲突评估',
          optimizationRecommendations: '这是优化建议'
        };
        
        // 更新store
        this.setNetworkData(networkData);
        this.setRadarData(radarData);
        this.setTimelineData(timelineData);
        this.setAnalysisData(analysisData);
        this.finishLoading();
      } catch (error) {
        this.setError(error instanceof Error ? error.message : '加载数据失败');
        console.error('加载分析数据失败:', error);
      }
    },
    
    // 重置store
    resetStore() {
      this.networkData = { nodes: [], links: [] };
      this.radarData = { categories: [], values: [] };
      this.timelineData = [];
      this.analysisData = null;
      this.isLoading = false;
      this.error = null;
    }
  }
});
